package 分类.二叉树;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class 寻找重复的子树 {
    HashMap<String, Integer> hashMap=new HashMap<>();
    List<TreeNode> res=new ArrayList<>();
    public List<TreeNode> findDuplicateSubtrees(TreeNode root) {

        find(root);
        return res;
    }

    String find(TreeNode root){
        if(root==null){
            return "#";
        }
        String left=find(root.left);
        String right=find(root.right);
        String result=left + "," + right + "," + root.val;
        int parm=hashMap.getOrDefault(result,0);
        if(parm==1){
            res.add(root);
        }
        hashMap.put(result,parm+1);
        return result;
    }
}
